#include "sagevis.h"

SageVis::SageVis(QGraphicsItem *parent) :
    QGraphicsWidget(parent)//,
{
    qDebug() << "sage vis open" << endl;

    //Make the program files directory, if it does not already exist
    qDebug() << "Application Dir PATH " << QApplication::applicationDirPath() << endl;
    QDir dir(QApplication::applicationDirPath().append("/programFiles/dataFiles"));
    if (!dir.exists()) {
        dir.mkpath(".");
    }

    setFlag( QGraphicsItem::ItemIsFocusable );//needed for keypress event

    timer = new QTimer(this);
    timer->setInterval( 100 );
    timer->start();
}

SageVis::~SageVis()
{
    for(int i = 0 ; i < widgets.size(); i++)
        if( widgets[i] )
            delete widgets[i];

    delete timer;

    //remove programFiles data directory
    //QDir dir;
    //dir.removeRecursively( QApplication::applicationDirPath().append("/programFiles/dataFiles") );
}

//this function launches a vis widget, given the path to a data file
//and a type- eg. which application should be launched with this data
//hopefully I will eventually be able to catch and detect which vis application is desired
//based on the file type
bool SageVis::launchWidget(QString type, QString pathToData)
{
    type = type.toLower();//first make sure lower case, so won't miss calls
    if( type.contains( "spatialwidget") )//this is a spatial / map widget for the Kenya course
    {
        qDebug() << "launching " << type << " data at: " << pathToData <<  endl;
        //widgets << new SpatialWidget(this);
        //widgets.at( widgets.size()-1)->loadData( pathToData );
        //connect(&timer, SIGNAL(timeout()), &widgets.at(widgets.size()-1), SLOT(advance()));
    }
    if( type.contains("bacterialcontigbrowser") )//this is a first approach to the bac contig genome browser
    {
        qDebug() << "launching " << type << " data at: " << pathToData <<  endl;
        widgets << new BacterialContigBrowser(this, this);
        widgets.at(widgets.size()-1)->loadData( pathToData );
        connect(timer, SIGNAL(timeout()), widgets.at(widgets.size()-1), SLOT(advance()));
    }

    //widgets << new VisBaseWidget( this );


    return true;
}

//turned off so I can finish developing it
//this function launches a vis widget,
// that lacks a data file- eg controllers, etc
bool SageVis::launchWidget(QString type ) //data less type
{
    type = type.toLower();//first make sure lower case, so won't miss calls
    if( type.contains( "colorcontrollerbox") )//this is a spatial / map widget for the Kenya course
    {
        qDebug() << "launching " << type <<  endl;
        widgets << new ColorControllerBox(this, this);

        widgets[widgets.size()-1]->setPos( -700, -500);
        widgets[widgets.size()-1]->setVisible(false);
    }
    if( type.contains("bacterialcontigbrowser") )//this is a first approach to the bac contig genome browser
    {
        QString pathToData;

        QFileDialog *fileDialog = new QFileDialog;
        QString gffFileName = fileDialog->getOpenFileName(0, "Select GFF2 File");
        QString fastaFileName = fileDialog->getOpenFileName(0, "Select FASTA File");

        qDebug() <<"gff " << gffFileName << "fasta " << fastaFileName << endl;

        qDebug() << "launching " << type << " data at: " << gffFileName << fastaFileName <<  endl;
        widgets << new BacterialContigBrowser(this, this);
        BacterialContigBrowser* temp = dynamic_cast<BacterialContigBrowser*>(widgets.at( widgets.size()-1));
        temp->loadData( gffFileName, fastaFileName );
        connect(timer, SIGNAL(timeout()), widgets.at(widgets.size()-1), SLOT(advance()));
    }

    return true;
}

